/***外部资源和样式***/ 
import { connect } from 'react-redux';
import { HashRouter as Router, Route, Switch, Redirect, Prompt } from 'react-router-dom';
import Index from './index';
import 'antd/dist/antd.less';
/***懒加载与loading***/ 
import Bundle from './framework/router/Bundle';
import Loading from 'components/Loading/Loading';

/***平台主页及其内容***/ 
import Admin from './admin';
import Home from 'bundle-loader?lazy&name=home!pages/home';//首页
import Demo from 'bundle-loader?lazy&name=demo!pages/demo';//demo
/*系统管理*/ 
import Menu from 'bundle-loader?lazy&name=menu!pages/sys/menu';//菜单管理
/*用户中心*/ 
import Account from 'pages/account';
import Login from 'bundle-loader?lazy&name=login!pages/login';
import Reg from 'bundle-loader?lazy&name=reg!pages/account/reg';
/*消息中心*/ 
import MessageList from 'bundle-loader?lazy&name=messageList!pages/message/messageList';//消息列表
import MessageAdd from 'bundle-loader?lazy&name=messageList!pages/message/messageAdd';//消息列表
/*404*/ 
import NotFound from './pages/notFound/NotFound';//404

const createComponent = (component) => (props) => (
  <Bundle load={component}>
      {
          (Component) => Component ? <Component {...props} /> : <Loading/>
      }
  </Bundle>
);


class PrivateRoute extends React.Component {
  //受保护组件
  render() {
      if (this.props.isLogin) {
          return (
              <Route path={this.props.path} component={createComponent(this.props.component)}/>
          )
      } else {
          return (
              <Redirect from={this.props.path} to='/login'/>
          )
      }
  }
}

class IRouter extends React.Component {

  render() {
    return (
      <Router>
        <Index>
          <Switch>
            <Route path="/login" component={createComponent(Login)}></Route>
            <Route path="/account" render={() =>
              <Account>
                <Switch>
                  <Route path="/account/reg" component={createComponent(Reg)}></Route>
                  <Route component={NotFound} />
                </Switch>
              </Account>
            }></Route>
            {/* 为了/加载到 */}
            <Route path="/" render={() =>
              <Admin>
                <Switch>
                  <PrivateRoute isLogin={this.props.isLogin} path="/home" component={Home}/>
                  <PrivateRoute isLogin={this.props.isLogin} path="/demo" component={Demo}/>
                  <PrivateRoute isLogin={this.props.isLogin} path="/sys/menu" component={Menu}/>
                  <PrivateRoute isLogin={this.props.isLogin} path="/message/messageList" component={MessageList}/>
                  <PrivateRoute isLogin={this.props.isLogin} path="/message/messageAdd/:id" component={MessageAdd}/>
                  <Redirect to="home"></Redirect>
                  {/* <Route component={NotFound} /> */}
                </Switch>
              </Admin>
            }></Route>
          </Switch>
        </Index>
      </Router>
    )
  }
}


const mapStateToProps = state => {
  return {
    isLogin: state.isLogin
  }
}
export default connect(mapStateToProps)(IRouter);